﻿using CommunityToolkit.Mvvm.Input;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;

namespace TestApp
{
    //RoutedCommand
    //ApplicationCommands
    //EditingCommands

    public class Window1ViewModel : INotifyPropertyChanged
    {
        public ICommand UpdateCommand { get; }
        public ICommand MyCommand { get; }
        public ICommand MyCommand2 { get; }

        private string _commandText = "初始值（Command）";


        public string InputText { get; set; } = "111";
        public ICommand SubmitCommand { get; }

        /// <summary>
        /// TextBox 的绑定属性
        /// </summary>
        public string CommandText
        {
            get => _commandText;
            set
            {
                _commandText = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 构造函数初始化命令
        /// </summary>
        public Window1ViewModel()
        {
            UpdateCommand = new RelayCommand(UpdateText);
            MyCommand = new RelayCommand(ExecuteMyCommand);
            MyCommand2 = new RelayCommand(ExecuteMyCommand);
            SubmitCommand = new RelayCommand<string>(ExecuteSubmit);
        }

        private void ExecuteSubmit(string? text)
        {
            if (text is not null)
            {
                MessageBox.Show($"文本内容:{text}"); // 输出: "Hello World"
            }
        }

        private void UpdateText()
        {
            CommandText = "命令：通过 Command 更新内容";
        }
        private void ExecuteMyCommand(string parameter)
        {
            MessageBox.Show(parameter); // 输出: "Hello World"
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null!) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    /// <summary>
    ///1.自定义方式Command
    /// </summary>
    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool>? _canExecute;
        private readonly Action<string> executeMyCommand;

        public RelayCommand(Action<string> executeMyCommand)
        {
            this.executeMyCommand = executeMyCommand;
        }

        public RelayCommand(Action execute, Func<bool>? canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public event EventHandler? CanExecuteChanged;

        public bool CanExecute(object? parameter) => _canExecute?.Invoke() ?? true;

        public void Execute(object? parameter) => _execute();
    }


    // 2. 定义包含RoutedCommand的静态类 Save方法
    public static class AppCommands
    {

        public static readonly RoutedUICommand SaveDocument = new RoutedUICommand(
            "Save Document",
            nameof(SaveDocument),
            typeof(AppCommands),
            new InputGestureCollection
            {
            new KeyGesture(Key.S, ModifierKeys.Control) // Ctrl+S
            });
    }

}
